<template>
    <div class="app-container">
        <el-form ref="searchForm" inline :model="searchForm" style="position:relative;">
            <div style="text-align: right;margin-bottom: 20px;"><el-button @click="addClick" type="primary">添加产品模型</el-button></div>
        </el-form>
        <el-table :data="tableData" border style="width: 100%;margin-bottom: 20px;">
            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
            <el-table-column prop="name" label="名称" align="center"></el-table-column>
            <el-table-column prop="description" label="属性描述" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="sort" label="顺序值" align="center"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" align="center" width="160"></el-table-column>
            <el-table-column label="操作" width="220" align="center" fixed="right">
                <template slot-scope="scope">
                    <el-button @click.stop="editClick(scope.row,2)" type="primary" plain size="mini">详情</el-button>
                    <el-button @click.stop="editClick(scope.row,1)" type="primary" plain size="mini">编辑</el-button>
                    <el-button @click.stop="delClick(scope.row)" type="danger" plain size="mini">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @current-change="getList" layout="total, prev, pager, next, jumper" :total="pageData.totalElements"></el-pagination>
        <!-- 弹框 -->
        <el-dialog :visible.sync="showDialog" width="800px">
            <el-form ref="dataForm" :rules="rules" :model="dataForm" label-position="left" label-width="120px" style="margin: 0px 50px;">
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="dataForm.name" :disabled="popStatus == 2" placeholder="必填项。请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="描述：" prop="description">
                    <el-input v-model="dataForm.description" type="textarea" :rows="4" :disabled="popStatus == 2" placeholder="必填项。请输入描述内容"></el-input>
                </el-form-item>
                <el-form-item label="顺序值：" prop="sort">
                    <el-input v-model="dataForm.sort" type="number" :disabled="popStatus == 2" placeholder="请输入顺序值"></el-input>
                </el-form-item>
                <el-button :disabled="popStatus == 2" @click="addSKU" type="primary" size="mini" plain icon="el-icon-circle-plus-outline">添加属性</el-button>
                <el-row v-for="(item, ind) in dataForm.productPropDtoList" :key="ind" style="text-align: center;margin: 10px 0px;">
                    <el-col :span="2" style="padding-top: 10px;">属性</el-col>
                    <el-col :span="14">
                        <el-input v-model="item.propName" placeholder="请输入产品属性" :disabled="popStatus == 2"></el-input>
                        <el-row v-for="(value,index) in item.productPropValueEntityList" :key="index">
                            <el-col :span="10" style="padding: 9px 5px 0;"><el-input v-model="value.propValue" placeholder="请输入属性值" :disabled="popStatus == 2"></el-input></el-col>
                            <el-col :span="10" style="padding: 9px 5px 0;"><el-input v-model="value.tooltip" placeholder="请输入提示信息" :disabled="popStatus == 2"></el-input></el-col>
                            <el-col :span="4"><el-button :disabled="popStatus == 2" @click="delSkuV(ind,index)" type="danger" plain size="mini" style="margin-top: 13px;">删除属性值</el-button></el-col>
                        </el-row>
                    </el-col>
                    <el-col :span="8" style="padding: 4px 0px 0px 5px;">
                        <el-button :disabled="popStatus == 2" @click="delSku(ind)" type="danger" size="mini">删除属性</el-button>
                        <el-button :disabled="popStatus == 2" @click="addSKUValue(ind)" type="primary" size="mini" plain icon="el-icon-circle-plus-outline">添加属性值</el-button>
                    </el-col>
                </el-row>
                <el-form-item style="text-align: center;margin-top: 20px;">
                    <el-button @click="addSubmit('dataForm')" type="primary" v-show="popStatus != 2">保存</el-button>
                    <el-button @click="cancelClick('dataForm')" v-show="popStatus != 2">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import { addProp, findProp, editProp, detailProp, delProp } from '@/api/product/product-property'
import { uploadPic, getPic } from '@/api/uploadPic'
import { getCookie, setCookie } from '@/utils/auth'
export default {
    name: 'Spu',
    data() {
        return{
            tableData: [],
            nowTit: '',
            nowId: '',
            showDialog: false,
            dataForm: {
                name: '',
                description: '',
                sort: '',
                productPropDtoList: []
            },
            rules: {
                name: [
                    { required: true, message: '请输入', trigger: 'blur' }
                ],
                description: [
                    { required: true, message: '请输入', trigger: 'blur' }
                ],
            },
            searchForm: {},
            nowPage: 0,
            pageSize: 10,
            imageUrl: '',
            formData: {},
            popStatus: 0,  //0为添加，1为修改，2为详情
            pageData: {},
            uploadInd: 0,
        }
    },
    methods: {
        //默认函数
        init(){
            let _this = this;
            //获取列表信息
            findProp(this.nowPage, this.pageSize).then(response => {
                if(response.code == 200){
                    _this.pageData = response.object;
                    _this.tableData = response.object.content;
                }else{
                    this.$message.error(response.message);
                }
            })
        },
        //添加
        addClick(){
            this.popStatus = 0;
            this.showDialog = true;
            this.dataForm.name = '';
            this.dataForm.sort = '';
            this.dataForm.description = '';
            this.dataForm.productPropDtoList = [];
            if(this.dataForm.id){
                delete this.dataForm.id;
            }
        },
        //提交添加
        addSubmit(formName){
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //提交
                    //判断 productPropDtoList里value不能为空且productPropDtoList不能为空
                    if(_this.dataForm.productPropDtoList.length == 0){
                        this.$message.error('请添加属性');
                        return
                    }else{
                        for (let key=0;key<_this.dataForm.productPropDtoList.length;key++) {
                            if(_this.dataForm.productPropDtoList[key].propName == ''){
                                this.$message.error('请输入属性');
                                return
                            }
                            if(_this.dataForm.productPropDtoList[key].productPropValueEntityList.length == 0){
                                this.$message.error('请添加属性值');
                                return
                            }else{
                                for (let i=0;i<_this.dataForm.productPropDtoList[key].productPropValueEntityList.length;i++) {
                                    if(_this.dataForm.productPropDtoList[key].productPropValueEntityList[i].propValue == ''){
                                        this.$message.error('请输入属性值');
                                        return
                                    }
                                    if(_this.dataForm.productPropDtoList[key].productPropValueEntityList[i].tooltip == ''){
                                        this.$message.error('请输入提示信息');
                                        return
                                    }
                                }
                            }
                        }
                    }
                    if(_this.popStatus == 0){  //添加
                        addProp(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })
                    }else if(_this.popStatus == 1){  //修改
                        _this.dataForm.id = _this.nowId;
                        editProp(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })
                    }
                    
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //删
        delClick(row){
            let _this = this;
            this.$confirm('确定删除 '+row.name+' 吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //删除
                delProp(row.id).then(response => {
                    if(response.code == 200){
                        this.$message.success(response.message);
                        _this.init();
                    }else{
                        this.$message.error(response.message);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });          
            });
        },
        //编辑
        editClick(row,type){
            const _this = this;
            _this.popStatus = type;
            _this.nowId = row.id;
            _this.dataForm.productPropDtoList = [];
            detailProp(row.id).then(response => {
                if(response.code == 200){
                    _this.dataForm.name = response.object.name;
                    _this.dataForm.sort = response.object.sort;
                    _this.dataForm.description = response.object.description;
                    const list = response.object.productPropEntityVoList;
                    if(list){
                        for(let i=0;i<list.length;i++){
                            _this.dataForm.productPropDtoList.push({propName: list[i].propName,productPropValueEntityList: []});
                            if(list[i].productPropValueEntityList){
                                for(let j=0;j<list[i].productPropValueEntityList.length;j++){
                                    _this.dataForm.productPropDtoList[i].productPropValueEntityList.push({propValue: list[i].productPropValueEntityList[j].propValue,tooltip: list[i].productPropValueEntityList[j].tooltip });
                                }
                            }
                        }
                    }
                }else{
                    this.$message.error(response.message);
                }
            })
            this.showDialog = true;
        },
        //取消
        cancelClick(formName){
            this.showDialog = false;
            this.$refs[formName].resetFields();
        },
        //分页
        getList(val) {
            this.nowPage = val - 1;
            this.init();
        },
        //添加属性
        addSKU(){
            this.dataForm.productPropDtoList.push({propName: '',productPropValueEntityList: []});
        },
        //添加属性值
        addSKUValue(ind){
            this.dataForm.productPropDtoList[ind].productPropValueEntityList.push({propValue: '', tooltip: ''});
        },
        //删除属性
        delSku(ind){
            this.dataForm.productPropDtoList.splice(ind,1);
        },
        //删除属性值
        delSkuV(ind,index){
            this.dataForm.productPropDtoList[ind].productPropValueEntityList.splice(index,1);
        },
    },
    mounted(){
        this.init()
    }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 60px;
    height: 60px;
    line-height: 60px;
    text-align: center;
  }
  .avatar {
    width: 60px;
    height: 60px;
    display: block;
  }
</style>